#ifndef OFX_IMAGE
#define OFX_IMAGE

#include "ofMain.h"
#include "ofxCvFilter.h"
#include "console.h"
#include "imageTool.h"

class ofxImage : public ofImage{

public:
	//Constructors + destructors
	ofxImage();
	
	virtual bool loadImage(string filename);
	bool loadImage(string filename, string basedir, int type);
	
	virtual void setImageType(int type);
	virtual void draw(float x, float y);
	virtual void draw(float x, float y, float width, float heigth);
	void draw();
	unsigned char& operator[](int i);
	void commitPixelOps();
	void commitFilterOps();
	void restore();
	UCHAR* getPixels();

	//Filtering options
	void setFilterSize(int width, int height);
	filterProperties getFilterProperties();
	void changeFilterSize(int widthDelta, int heigthDelta);
	void setRoi(int x, int y, int w, int h);

	//Filtering operations
	void erode(int itterations=1);
	void dilate(int itterations=1);
	void open(int itterations=1);
	void blur(int value=3);
	void blurgaussian(int value=3);
	void close(int itterations=1);
	void blackhat(int itterations=1);
	void tophat(int itterations=1);	
	void threshold(int value, bool invert=false);


	//Variables
	int pixelcount;
	string name;

private:

	void init();
	void ocvSetup();
	void ocvSetup2();
	void prepareDraw();
	bool ocvFormatCompatible();
	void prepareFilterOp();


	//metadata
	string sourcename;

	//flags
	bool ocvImgDirty;
	bool ocvImgOutdated;

	//image versions
	ofImage original;

	//pixel arrays
	unsigned char* pixels;
	unsigned char* grayscale_pixel;

	//openCv filtering
	ofxCvImage* ocvImg;

	IplImage* ocvImage;

	ofxCvFilter filter;
	ofxCvFilterkernel filterKernel;
};

#endif